package com.hymjweb.demo.hymj.bill_code_manage.b_native.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.bill_code_manage.b_native.bean.BillNativeCodePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.bill_code_manage.b_native.mapper
 * @Author: luoge
 * @CreateTime: 2020-10-31 20:29
 * @Description:
 */
@Mapper
@Component
public interface BillNativeCodeMapper {

    class BillNativeCodeProvider {

        public String findAll(Map map) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_native_bill_code t");
                WHERE("1=1");
            }}.toString();

            if (map != null) {
                String keyId = DataTypeUtil.toNotNullString(map.get("keyId"));
                Long keyUseStatusId = DataTypeUtil.toLong(map.get("keyUseStatusId"), -1L);
                String keyDepartmentId = DataTypeUtil.toNotNullString(map.get("keyDepartmentId"));
                String keyUserId = DataTypeUtil.toNotNullString(map.get("keyUserId"));
                String keyStartReceiveDate = DataTypeUtil.toNotNullString(map.get("keyStartReceiveDate"));
                String keyEndReceiveDate = DataTypeUtil.toNotNullString(map.get("keyStartReceiveDate"));
                // select * from test where date_format(create_time,'%Y-%m-%d') between '2019-03-05' and '2019-03-08'
                if (keyId != null && !"".equals(keyId)) {
                    sql += " and t.id=" + DataTypeUtil.quoteStr(keyId);
                }
                if (keyUseStatusId != null && keyUseStatusId != -1L) {
                    sql += " and t.use_status_id = " + keyUseStatusId;
                }
                if (keyDepartmentId != null && !"".equals(keyDepartmentId)) {
                    sql += " and t.generate_department_id=" + DataTypeUtil.quoteStr(keyDepartmentId);
                }
                if (keyUserId != null && !"".equals(keyUserId)) {
                    sql += " and t.generate_user_id=" + DataTypeUtil.quoteStr(keyUserId);
                }
                if (keyStartReceiveDate != null && keyEndReceiveDate != null && !"".equals(keyEndReceiveDate) && !"".equals(keyStartReceiveDate)) {
                    sql += " and (date_format(generate_date,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyStartReceiveDate) + " and " + DataTypeUtil.quoteStr(keyEndReceiveDate) + ")";
                }
            }

            return sql;
        }

        public String checkBillCodeIdUseStatus(String billCodeId, Long useStatusId, String generateDepartmentId) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_native_bill_code t");
                WHERE("1=1");
            }}.toString();
            sql += " and t.id=" + DataTypeUtil.quoteStr(billCodeId);
            sql += " and t.use_status_id=" + useStatusId;
            sql += " and t.use_department_id is null";
            sql += " and t.use_user_id is null";
            return sql;
        }

        public String getBillNativeCodePojoById(String billCodeId) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_native_bill_code t");
                WHERE("1=1 and t.id = #{billCodeId}");
            }}.toString();
            return sql;
        }

        public String update(BillNativeCodePojo billNativeCodePojo) {
            String sql = new SQL() {{
                UPDATE("b_native_bill_code t");
                SET("t.use_status_id = #{useStatusId}");
                SET("t.generate_department_id = #{generateDepartmentId}");
                SET("t.generate_date = #{generateDate}");
                SET("t.generate_user_id = #{generateUserId}");
                SET("t.use_department_id = #{useDepartmentId}");
                SET("t.use_user_id = #{useUserId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String billCode) {
            String sql = new SQL() {{
                DELETE_FROM("b_native_bill_code t");
                WHERE("t.id = #{billCode}");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "billNativeCodeMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "use_status_id", property = "useStatusId"),
            @Result(column = "generate_department_id", property = "generateDepartmentId"),
            @Result(column = "generate_date", property = "generateDate"),
            @Result(column = "generate_user_id", property = "generateUserId"),
            @Result(column = "use_department_id", property = "useDepartmentId"),
            @Result(column = "use_user_id", property = "useUserId")
    })
    @SelectProvider(type = BillNativeCodeProvider.class, method = "findAll")
    List<BillNativeCodePojo> findAll(Map map);


    @ResultMap("billNativeCodeMapId")
    @SelectProvider(type = BillNativeCodeProvider.class, method = "checkBillCodeIdUseStatus")
    BillNativeCodePojo checkBillCodeIdUseStatus(String billCodeId, Long useStatusId, String generateDepartmentId);

    @ResultMap("billNativeCodeMapId")
    @SelectProvider(type = BillNativeCodeProvider.class, method = "getBillNativeCodePojoById")
    BillNativeCodePojo getBillNativeCodePojoById(String billCodeId);

    /**
     * 分单号批量插入
     *
     * @param canSaveList
     * @return
     */
    @Insert(
            "<script>" +
                    "insert into b_native_bill_code(id, use_status_id, generate_department_id, generate_date, generate_user_id) values " +
                    "<foreach collection='canSaveList' item='billNativeCodePojo' separator=','>" +
                    "(#{billNativeCodePojo.id},#{billNativeCodePojo.useStatusId},#{billNativeCodePojo.generateDepartmentId},#{billNativeCodePojo.generateDate},#{billNativeCodePojo.generateUserId})" +
                    "</foreach>" +
                    "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("canSaveList") List<BillNativeCodePojo> canSaveList);

    @UpdateProvider(type = BillNativeCodeProvider.class, method = "update")
    int update(BillNativeCodePojo billNativeCodePojo);

    @DeleteProvider(type = BillNativeCodeProvider.class, method = "delete")
    int delete(String billCode);


}
